﻿using System.Collections.Generic;
using System.Linq;

namespace SydEnergiPrototype.Logic
{
    public class Subject
    {
        private static List<IObserver> _registeredObservers;

        public Subject()
        {
            _registeredObservers = new List<IObserver>();
        }

        public static void RegisterObserver(IObserver observer)
        {
            if(_registeredObservers.Any(o => o.GetType() == observer.GetType()))
            {
                var index = _registeredObservers.IndexOf(_registeredObservers.Single(o => o.GetType() == observer.GetType()));
                _registeredObservers[index] = observer;
                return;
            }

            _registeredObservers.Add(observer);
        }

        protected void NotifyObservers()
        {
            foreach (var registeredObserver in _registeredObservers)
            {
                registeredObserver.Update();
            }

            _registeredObservers.Clear();
        }
    }
}
